/*
 * 版权所有(c)  1952110078@qq.com  2022 - 2022
 */

/*
 * 版权所有(c)  1952110078@qq.com  2022 - 2022
 */

package com.ldc.commonlibs.views;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.ldc.commonlibs.R;
import com.ldc.commonlibs.common.PageStateCode;
import com.ldc.commonlibs.listener.OnRowListener;

/**
 * @author liudc
 * @since 2022/7/8 23:26
 */
public class StateViewLayout extends FrameLayout implements StateViewLayoutImpl {
    private Context context;
    private View emptyView;
    private View contentView;
    private View successView;
    private View loadingView;
    private View errorView;
    private OnRowListener<View> onTryListener;

    /**
     * 点击事件
     *
     * @param onTryListener
     */
    public void addOnTryListener(OnRowListener<View> onTryListener) {
        this.onTryListener = onTryListener;
    }

    /**
     * 初始化
     *
     * @param activity activity
     * @return 转态视图
     */
    public static StateViewLayout init(@NonNull Activity activity) {
        ViewGroup viewGroup = activity.findViewById(android.R.id.content);
        View contentView = viewGroup.getChildAt(0);
        return init(contentView);
    }

    /**
     * 初始化
     *
     * @param activity activity
     * @param viewId   viewId
     * @return 状态视图
     */
    public static StateViewLayout init(@NonNull Activity activity, @IdRes int viewId) {
        ViewGroup viewGroup = activity.findViewById(android.R.id.content);
        View contentView = viewGroup.findViewById(viewId);
        return init(contentView);
    }

    /**
     * 初始化
     *
     * @param fragment fragment
     * @param viewId   viewId
     * @return 状态视图
     */
    public static StateViewLayout init(@NonNull Fragment fragment, @IdRes int viewId) {
        View view = fragment.getView();
        if (view == null) {
            throw new NullPointerException("contentView is null");
        }
        View contentView = view.findViewById(viewId);
        return init(contentView);
    }

    /**
     * 初始化
     *
     * @param contentView 内容视图
     * @return 状态视图
     */
    public static StateViewLayout init(View contentView) {
        if (contentView == null) {
            throw new NullPointerException("ContentView is null");
        }
        ViewGroup parent = (ViewGroup) contentView.getParent();
        // 获取大小
        ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
        // 获取下标
        int index = parent.indexOfChild(contentView);
        // 删除视图
        parent.removeView(contentView);
        // 初始化状态视图
        StateViewLayout stateView = new StateViewLayout(parent.getContext());
        stateView.addView(contentView);
        stateView.contentView = contentView;
        //将状态视图添加到rootView
        parent.addView(stateView, index, layoutParams);
        return stateView;
    }

    public StateViewLayout(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public void showContentView() {
        showView(PageStateCode.CONTENT_VIEW);
    }

    @Override
    public void showSuccessView() {
        showView(PageStateCode.SUCCESS_VIEW);
    }

    @Override
    public void showErrorView() {
        showView(PageStateCode.ERROR_VIEW);
    }

    @Override
    public void showEmptyView() {
        showView(PageStateCode.EMPTY_VIEW);
    }

    @Override
    public void showLoadingView() {
        showView(PageStateCode.LOADING_VIEW);
    }

    private void showView(PageStateCode code) {
        if (indexOfChild(contentView) == -1) {
            addView(contentView);
        }
        loadingView = getLoadingView();
        if (indexOfChild(loadingView) == -1) {
            addView(loadingView);
        }
        successView = getSuccessView();
        if (indexOfChild(successView) == -1) {
            addView(successView);
            successView.findViewById(R.id.tv_try).setOnClickListener(v -> {
                if (onTryListener != null) {
                    onTryListener.onRowListener(v);
                }
            });
        }
        errorView = getErrorView();
        if (indexOfChild(errorView) == -1) {
            addView(errorView);
            errorView.findViewById(R.id.tv_try).setOnClickListener(v -> {
                if (onTryListener != null) {
                    onTryListener.onRowListener(v);
                }
            });
        }
        emptyView = getEmptyView();
        if (indexOfChild(emptyView) == -1) {
            addView(emptyView);
            emptyView.findViewById(R.id.tv_try).setOnClickListener(v -> {
                if (onTryListener != null) {
                    onTryListener.onRowListener(v);
                }
            });
        }
        this.contentView.setVisibility(code == PageStateCode.CONTENT_VIEW ? View.VISIBLE : GONE);
        this.loadingView.setVisibility(code == PageStateCode.LOADING_VIEW ? View.VISIBLE : GONE);
        this.successView.setVisibility(code == PageStateCode.SUCCESS_VIEW ? View.VISIBLE : GONE);
        this.errorView.setVisibility(code == PageStateCode.ERROR_VIEW ? View.VISIBLE : GONE);
        this.emptyView.setVisibility(code == PageStateCode.EMPTY_VIEW ? View.VISIBLE : GONE);
    }

    private View getLoadingView() {
        return loadingView == null ? getView(R.layout.layout_view_loading) : loadingView;
    }

    private View getEmptyView() {
        return emptyView == null ? getView(R.layout.layout_view_empty) : emptyView;
    }

    private View getSuccessView() {
        return successView == null ? getView(R.layout.layout_view_success) : successView;
    }

    private View getErrorView() {
        return errorView == null ? getView(R.layout.layout_view_error) : errorView;
    }

    private View getView(@LayoutRes int layoutId) {
        View view = LayoutInflater.from(context).inflate(layoutId, null, false);
        FrameLayout.LayoutParams layoutParams = new LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT
        );
        view.setLayoutParams(layoutParams);
        return view;
    }
}
